﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic

Namespace Pic
    Public Class Gifts
        Inherits clsBase
#Region "Private Variables"
        Private _Id As Integer
        Private _Price As Double
#End Region
#Region "Properties"
        Public Property Id As Integer
            Set(value As Integer)
                _Id = value
            End Set
            Get
                Return _Id
            End Get
        End Property
        Public Property Price As Double
            Set(value As Double)
                _Price = value
            End Set
            Get
                Return _Price
            End Get
        End Property
#End Region
#Region "Constructor"
        Public Sub New(ByVal Id_ As Integer, ByVal Price_ As Double)
            Me.Id = Id_
            Me.Price = Price_
        End Sub
#End Region
#Region "Methods"
        Private Shared Function GetGiftsFromReader(ByVal reader_ As IDataReader) As Gifts
            Dim rtrn As Gifts = New Gifts(reader_("Id"), reader_("Price"))
            Return rtrn
        End Function
        Public Shared Function GetGiftById(ByVal Id_ As Integer) As Gifts
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select * from tbl_Gifts where id=@Id"
            Dim reader As IDataReader
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text

                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                reader = ExecuteReader(cmd, CommandBehavior.SingleRow)
                If reader.Read Then
                    Return GetGiftsFromReader(reader)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Private Shared Function GetGiftsListFromReader(ByVal reader As IDataReader) As List(Of Gifts)
            Dim rtrnList As New List(Of Gifts)
            Do While reader.Read
                rtrnList.Add(GetGiftsFromReader(reader))
            Loop
            Return rtrnList
        End Function
        Public Shared Function GetGiftsListByFilter(ByVal Filtre_ As String, ByVal FiltreOrder_ As String) As List(Of Gifts)
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "select * from website1.dbo.tbl_Gifts where " & Filtre_ & "' order by " & FiltreOrder_ & " asc"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                Return GetGiftsListFromReader(ExecuteReader(cmd, CommandBehavior.Default))
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function InsertGift(ByVal Id_ As String, ByVal Price_ As Double) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "insert into tbl_Gifts(id,price) values(@Id,@Price)"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = Id_
                cmd.Parameters.Add("@Price", SqlDbType.Float).Value = Price_
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function UpdateGift(ByVal Id_ As String, ByVal Price_ As Double) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "update tbl_Gifts set price=@Price where id=@Id"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = Id_
                cmd.Parameters.Add("@Price", SqlDbType.Float).Value = Price_
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function DeleteGift(ByVal Id_ As Integer) As Boolean
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select count(1) from tbl_Gifts Where id=@Id"
            Dim count As Integer
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                count = ExecuteScalar(cmd)
                If count = 0 Then
                    Return False
                Else
                    cmd.CommandText = "Delete from tbl_Gifts Where id=@Id"
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                    Return ExecuteNonQuery(cmd)

                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        'Public Shared Function SilinebilirMi(ByVal ProjeID_ As Integer) As Boolean
        '    Dim conn As New SqlConnection(ConnectionString)
        '    Dim veriString As String = "Select count(1) from tblSondajlar where ProjeID=" & ProjeID_
        '    Dim count As Integer
        '    Try
        '        conn.Open()
        '        Dim cmd As New SqlCommand(veriString, conn)
        '        count = ExecuteScalar(cmd)
        '        If count = 0 Then
        '            Return True
        '        Else
        '            Return False
        '        End If
        '    Catch ex As Exception
        '        Throw ex
        '    Finally
        '        conn.Close()
        '    End Try
        'End Function
#End Region

    End Class
End Namespace


